<template>
  <view class="auth-box"></view>
</template>

<script>
  import qs from '@/utils/qs.js'
  import mixin from '@/mixins/auth.js';
  
  export default {
    mixins: [mixin],
    data() {
      return {
        reURL: '',
        loginState: '',
        loginCode: '',
      }
    },
    onLoad(e) {
      uni.showLoading({
        title: this.$t('login.authing') // todo
      })
        
      if (location.hash != '') {
        let hash = location.hash.slice(1);
        let params = hash.split('&');
        let auth = {};
        params.forEach(param => {
          let [key, val] = param.split('=');
          auth[key] = val; 
        })
        
        if (auth.access_token) {
          // this.login('google', auth.access_token)
          this.loginState = auth.state
          this.GoogleLoginHandle(auth)
          console.log(auth)
        }
      }
    },
    
    methods: {
      GoogleLoginHandle(params) {
        
          var id_token = googleUser.getAuthResponse().id_token;
          console.log(id_token)
        
          if (Object.keys(params).length > 0) {
            localStorage.setItem('oauth2-test-params', JSON.stringify(params) );
            if (params && params['access_token']) {
              var xhr = new XMLHttpRequest();
              xhr.open('GET',
                  'https://www.googleapis.com/drive/v3/about?fields=user&' +
                  'access_token=' + params['access_token']);
              xhr.onreadystatechange = function (e) {
                if (xhr.readyState === 4 && xhr.status === 200) {
                  console.log(xhr.response);
                } else if (xhr.readyState === 4 && xhr.status === 401) {
                  // Token invalid, so prompt for user permission.
                }
              };
              xhr.send(null);
            } else {
            }
          }
      }
    }
  }
</script>

<style>
  .auth-box {
    display: flex;
    align-items: center;
    height: 100vh;
    width: 100vw;
    justify-content: center;
  }
  .button-login {
    width: 654rpx;
    height: 98rpx;
    box-shadow: 0rpx 0rpx 13rpx 0rpx rgba(164, 217, 228, 0.2);
    background-size: contain;
  }
  .button-login-fb {
    background-image: url(../static/img/login/login_fb.png);
  }
</style>